<template>
    <div class="template-list" v-loading="loading">
        <div class="table-box">
            <el-table :data="tableData" ref="table" border style="width: 100%">
                <el-table-column width="120" prop="id" label="id"></el-table-column>
                <el-table-column width="120" prop="number" label="趣宝石数量"></el-table-column>
                <el-table-column min-width="120" label="类型">
                  <template slot-scope="scope">
                        <span>{{ scope.row.type === 0 ? '支出' : '收入' }}</span>
                    </template>
                </el-table-column>
                <el-table-column min-width="150" label="创建时间">
                    <template slot-scope="scope">
                        <span>{{ _formatDate(scope.row.createTime) }}</span>
                    </template>
                </el-table-column>
                <el-table-column min-width="120" prop="remark" label="描述"></el-table-column>
            </el-table>
        </div>
        <div class="tools">
            <el-pagination
                @size-change="sizeChange"
                @current-change="pageChange"
                :current-page="pageNum"
                :page-sizes="_pageSizes"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </div>
    </div>
</template>

<script>
import { formatDate } from '@/utils/utils'
export default {
    data () {
        return {
            tableData: [],
            pageNum: 1,
            pageSize: 30,
            total: 0,
            loading: false
        }
    },
    props: ['detail'],
    created () {
        if (this.detail && this.detail.userId) {
            this.getList(this.pageNum)
        }
    },
    methods: {
        // 获取列表
        async getList (pageNum) {
            if (this.loading) return
            this.pageNum = pageNum
            this.loading = true
            let params = {
                pageNum: pageNum,
                pageSize: this.pageSize,
                userId: this.detail.userId
                // userId: 100130
            }
            let res = await this.$http.get('/user/getUserDiamondRecord', {
                params
            })
            if (res.code === '0') {
                this.tableData = res.data
                this.total = res.count
            } else {
                this.$message({
                    type: 'error',
                    message: '系统异常，请稍后再试'
                })
            }
            this.loading = false
        },
        _formatDate (timestamp) {
            return formatDate(timestamp, 'YYYY-MM-DD HH:MM')
        },
        // 页码切换
        sizeChange (val) {
            this.pageNum = 1
            this.pageSize = val
            this.getList(1)
        },
        // 页容切换
        pageChange (val) {
            this.pageNum = val
            this.getList(this.pageNum)
        }
    }
}
</script>

<style lang="scss" scoped>
    .template-list {
        .template-option-box {
            max-width: 800px;
        }

        .table-box {
            margin: 20px 0;
            .point {
                display:inline-block;
                width:10px;
                height:10px;
                border-radius: 50%;
                background-color:#67C23A;
                margin-right:3px;
            }
            .red-point {
                background-color: #F56C6C;
            }
        }

        .tools {
            padding-right: 20px;
            display: flex;
            justify-content: flex-end;
        }
    }
</style>
